package cn.sfrog.common.obs;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.sfrog.common.oss.OssProperties;
import cn.sfrog.common.oss.service.OssTemplate;
import com.obs.services.ObsClient;
import com.obs.services.model.PutObjectResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


public class HwObsUtil {
    private static ObsClient obsClient;
    private static String bucketName;

    private static OssTemplate ossTemplate;
    private static OssProperties ossProperties;
    static {

        try {
            ObsProperties obsProperties = new ObsProperties();
            String endPoint = obsProperties.getEndPoint();
            String ak = obsProperties.getAk();
            String sk = obsProperties.getSk();
            bucketName = obsProperties.getBucket();
            // 创建OSSClient实例。
            obsClient = new ObsClient(ak, sk, endPoint);
        }catch (Exception e){
            e.printStackTrace();
        }

    }




    public static String uploadFile(File file) {
        //文件名
        String folder = "file/"; //doctorPhoto
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
        fileName = String.valueOf(Math.random() * 10000.0D).replace(".", "") + suffix;
        //以输入流的形式上传文件
        PutObjectResult putObjectResult = obsClient.putObject(bucketName, folder + fileName, file);
        System.out.println(putObjectResult);
        return putObjectResult.getObjectUrl();
    }

    public static String uploadFileOSS(MultipartFile multipartFile) {
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(multipartFile.getOriginalFilename());
        try {
            ossTemplate.putObject(ossProperties.getBucketName(), fileName, multipartFile.getInputStream());
        } catch (Exception e) {
        }
        return  String.format("%s/%s/%s", ossProperties.getEndpoint(), ossProperties.getBucketName(), fileName);
    }


    /**
     * 通过文件名判断并获取OBS服务文件上传时文件的contentType
     *
     * @param fileName 文件名
     * @return 文件的contentType
     */
    public static String getContentType(String fileName) {
        //文件的后缀名
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        if (".bmp".equalsIgnoreCase(fileExtension)) {
            return "image/bmp";
        }
        if (".gif".equalsIgnoreCase(fileExtension)) {
            return "image/gif";
        }
        if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension) || ".png".equalsIgnoreCase(fileExtension)) {
            return "image/jpeg";
        }
        if (".html".equalsIgnoreCase(fileExtension)) {
            return "text/html";
        }
        if (".txt".equalsIgnoreCase(fileExtension)) {
            return "text/plain";
        }
        if (".vsd".equalsIgnoreCase(fileExtension)) {
            return "application/vnd.visio";
        }
        if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
            return "application/vnd.ms-powerpoint";
        }
        if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
            return "application/msword";
        }
        if (".xml".equalsIgnoreCase(fileExtension)) {
            return "text/xml";
        }
        if (".avi".equalsIgnoreCase(fileExtension)) {
            return "video/avi";
        }
        if (".mp4".equalsIgnoreCase(fileExtension)) {
            return "video/mpeg4";
        }
        if (".wmv".equalsIgnoreCase(fileExtension)) {
            return "video/x-ms-wmv ";
        }
        //默认返回类型
        return "image/jpeg";
    }

}
